<?php

class Product extends AppModel {

	var $name = 'Product';
	
	var $belongsTo = array('Producer','Subcategory');
	var $hasMany = array('Orderable'=>array('foreignKey'=>'product_id'));

	var $useTable = 'product_list';
	
	var $primaryKey = 'product_id';
	
	function beforeSave() {
		if (isset($this->data['Product']['product_name'])) {
			$this->data['Product']['product_name'] = trim($this->data['Product']['product_name']);
		}
		if (isset($this->data['Product']['pricing_unit'])) {
			if (empty($this->data['Product']['ordering_unit']) || !($this->data['Product']['random_weight'])) {
				$this->data['Product']['ordering_unit'] = $this->data['Product']['pricing_unit'];
			}
		}
		return true;
	}
	
	function archive($id) {
		$this->id = $id;
		$this->saveField('archived',1);
		$this->saveField('donotlist',1);
	}
	
	function c_find($where = '1', $order = null, $limit = null, $page = 1) {
	
		if ($order === null) {
			$order = 'Product.product_name ASC';
		} else if (!strpos('product_name',$order)) {
			$order = $order . ', Product.product_name ASC';
		}
		
		if ($limit !== null) {
			$lower = $limit * ($page - 1);
			$upper = $limit;
			$limit = " LIMIT $lower, $upper";
		} else {
			$limit = '';
		}
		
		if (is_array($where)) {
			$where = implode(' AND ', $where);
		}
		
	
    $d = Configure::read('Delivery.delivery_id');
    if (Configure::read('Delivery.after_open') || Configure::read('Delivery.is_open')) {
			$sql = 'SELECT Orderable.id, Product.product_id, Product.producer_id, Product.product_name , Member.business_name , Member.username , Orderable.price , Orderable.priced_by , Orderable.ordered_by, Orderable.inventory_start, Orderable.inventory_left , Product.maximum_weight , Product.minimum_weight , Product.random_weight , Product.tax_exempt, Product.storage, Product.created, Product.modified, Product.unit_price, Product.pricing_unit, Subcategory.subcategory_id, Category.category_id, Category.category_name, Subcategory.subcategory_name, Product.detailed_notes'
       .' FROM members as Member , producers as Producer , orderables as Orderable, product_list as Product'
       .' LEFT JOIN subcategories AS Subcategory ON Product.subcategory_id = Subcategory.subcategory_id'
       .' LEFT JOIN categories AS Category ON Subcategory.category_id = Category.category_id'
       .' WHERE Orderable.delivery_id = '.$d.' '
       .' AND Product.product_id = Orderable.product_id '
       .' AND Product.donotlist = 0 '
       .' AND Producer.producer_id = Product.producer_id '
       .' AND Member.member_id = Producer.member_id '
       .' AND (' . $where
       .') ORDER BY '.$order . $limit;
		} else {
			$sql = 'SELECT Product.product_id, Product.producer_id, Product.product_name , Member.business_name , Member.username , Product.unit_price , Product.pricing_unit , Product.ordering_unit, Product.maximum_weight , Product.minimum_weight , Product.random_weight , Product.tax_exempt, Product.storage, Product.created, Product.modified, Subcategory.subcategory_id, Category.category_id, Category.category_name, Subcategory.subcategory_name, Product.detailed_notes'
       .' FROM members as Member , producers as Producer, product_list as Product'
       .' LEFT JOIN subcategories AS Subcategory ON Product.subcategory_id = Subcategory.subcategory_id'
       .' LEFT JOIN categories AS Category ON Subcategory.category_id = Category.category_id'
       .' WHERE Product.donotlist = 0 '
       .' AND Producer.producer_id = Product.producer_id '
       .' AND Member.member_id = Producer.member_id '
       .' AND (' . $where
       .') ORDER BY '.$order . $limit;
       
		}
		$results = $this->query($sql);
       
		return $results;
	}
	
	function c_find_any($where = '1', $order = null, $limit = null, $page = 1) {
	
		if ($order === null) {
			$order = 'Product.product_name ASC';
		} else if (!strpos('product_name',$order)) {
			$order = $order . ', Product.product_name ASC';
		}
		
		if ($limit !== null) {
			$lower = $limit * ($page - 1);
			$upper = $limit;
			$limit = " LIMIT $lower, $upper";
		} else {
			$limit = '';
		}
		
		$sql = 'SELECT Product.product_id, Product.producer_id, Product.product_name , Member.business_name , Product.unit_price , Product.pricing_unit , Product.ordering_unit, Product.maximum_weight , Product.minimum_weight , Product.random_weight , Product.tax_exempt, Product.storage, Product.inventory_on, Product.inventory, Product.donotlist, Subcategory.subcategory_id, Category.category_id, Category.category_name, Subcategory.subcategory_name, Product.detailed_notes'
       .' FROM members as Member , producers as Producer, product_list as Product'
       .' LEFT JOIN subcategories AS Subcategory ON Product.subcategory_id = Subcategory.subcategory_id'
       .' LEFT JOIN categories AS Category ON Subcategory.category_id = Category.category_id'
       .' WHERE Producer.producer_id = Product.producer_id '
       .' AND Member.member_id = Producer.member_id '
       .' AND (' . $where
       .') ORDER BY '.$order . $limit;
       
       $results = $this->query($sql);
       
       return $results;
	}
	
}

?>
